package com.interview.controller;

import com.interview.feign.FeignInterviewService;
import com.interview.pojo.Title;
import com.interview.utils.JsonTurnUtils;
import com.interview.utils.Result;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/feign_title")
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class FeignTitleController {

    @Autowired
    private FeignInterviewService feignTitleService;


    //http://localhost:8083/feign_title

    @PostMapping
    @HystrixCommand
    public String add(@RequestBody Title title){

        feignTitleService.add(title);
        return "添加成功";
    }
    //http://localhost:8083/feign_title/del
    @PostMapping("/del")
    @HystrixCommand
    public String del(@RequestBody Title title){
        feignTitleService.del(title);
        return "删除成功";
    }


    //http://localhost:8083/feign_title/up
    @PostMapping("/up")
    @HystrixCommand
    public String titleup(@RequestBody Title title){
        try {
            feignTitleService.titleup(title);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  "修改成功";
    }


    //http://localhost:8083/feign_title
    @GetMapping
    @HystrixCommand
    public String queryAll(){
        String s = null;
        try {
            Result<List<Title>> titleResult = feignTitleService.queryAll();
            s = JsonTurnUtils.objTurnJson( titleResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  s;
    }

    //http://localhost:8083/feign_title/count
    @GetMapping("/count")
    public String title_count(){
        int i = feignTitleService.title_count();
       String s = JsonTurnUtils.objTurnJson( i);
       return  s;
    }


    //http://localhost:8083/feign_title/query_one?titleid=3-2021yJ1cZ6fE10aW43tZ30h789T-778
    @GetMapping("/query_one")
    @HystrixCommand
    public String title_one(@RequestParam("titleid") String titleid){
        Title title = feignTitleService.title_one(titleid);
      String  s = JsonTurnUtils.objTurnJson( title);
      return  s;
    }


    //降级方法
    public String payment_Global_FallbackMethod() {
        String str = "<div style=\"width: 100%;text-align: center\">\n" +
                "    <h1 >对不起，网络太拥挤了！ 刷新重试-----Feign</h1>\n" +
                "</div>";
        return str;
    }



    //http://localhost:8083/feign_article/hystrix
    //测试 熔断是否触发
    @GetMapping("/hystrix")
    @ResponseBody
    @HystrixCommand
    public String testHystrix() throws InterruptedException {
        //这个触发了 feign-service 内的 超时处理
       Thread.sleep(5000);

       return  "超时";
    }

}
